package study.mybatis.support.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class ReflectUtils {

    public static Method getMethod(Class clazz, String mName) {
        Method[] methods = clazz.getDeclaredMethods();
        Method method = Arrays.stream(methods)
                .filter(m -> mName.equals(m.getName()))
                .findFirst()
                .get();
        return method;
    }

    /**
     * 调用方法
     */
    public Object invoke(Object obj, Method method, Object... params) {
        Object result = null;
        try {
            result = method.invoke(obj, params);
        } catch (InvocationTargetException | IllegalAccessException ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
